package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.dao.CompanyDao;
import com.coocaa.internship.dao.PositionInfoDao;
import com.coocaa.internship.pojo.Company;
import com.coocaa.internship.pojo.PositionInfo;
import com.coocaa.internship.service.PositionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: 凌贤
 * @Date: 2019-08-04
 * @Time: 19:45
 */
@Service
public class PositionInfoServicelmpl implements PositionInfoService {
    @Autowired
    PositionInfoDao positionInfoDao;

    @Autowired
    CompanyDao companyDao;

    @Override
    public ResultData selectPositionInfoById(Long positionId) {
        PositionInfo positionInfo = positionInfoDao.selectPositionInfoById(positionId);
        Company company = companyDao.selectCompanyById(positionInfo.getCompanyId());
        Map<String, Object> map = new HashMap<>();
        map.put("positionInfo", positionInfo);
        map.put("company", company);
        return ResultData.success(map);
    }

    @Override
    public ResultData selectAll() {
        return ResultData.success(positionInfoDao.selectAll());
    }

    @Override
    public ResultData insertPosition(PositionInfo positionInfo) {
        positionInfoDao.insertPostitionInfo(positionInfo);
        return ResultData.success(positionInfo);
    }

    @Override
    public ResultData deletePosition(PositionInfo positionInfo) {
        if (positionInfo.getPositionId() != null) {
            return ResultData.success(positionInfoDao.deleteByPositionId(positionInfo.getPositionId()));
        } else if (positionInfo.getPositionName() != null) {
            return ResultData.success(positionInfoDao.deleteByPositionName(positionInfo.getPositionName()));

        } else
            return ResultData.failed();
    }

    @Override
    public ResultData deletePosition(Long positionId) {
        return ResultData.success(positionInfoDao.deleteByPositionId(positionId));
    }

    @Override
    public ResultData selectByCompanyId(Long companyId) {
        return ResultData.success(positionInfoDao.selectByCompanyId(companyId));
    }

    @Override
    public ResultData updatePosition(PositionInfo positionInfo) {
        return ResultData.success(positionInfoDao.updateByPositionId(positionInfo));
    }
}
